﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using S4Store.Domain.Report;
using S4Store.Models.Chart;
using S4Store.Domain;
using S4Store.Models.Report;

namespace S4Store.Models.SellerReport
{
    public class SellerDataItemModel : BaseReportModel
    {
        public SellerDataItemModel()
        {
            SellerPersonReports = new List<SellerPersonReport>();
            SellerReportModel = new Domain.Report.SellerReport();
        }

        public SellerDataItemType ItemType { get; set; }

        public List<SellerPersonReport> SellerPersonReports { get; set; }
        public Domain.Report.SellerReport SellerReportModel { get; set; }

        public Line LineModel { get; set; }

        public List<String> GetViewItems()
        {
            switch (ItemType)
            {
                case SellerDataItemType.首次来店登记率:
                    {
                        return new List<String> { "首次来店接待批次","首次来店客户登记数","首次来店登记率"};
                        
                    }
                case SellerDataItemType.试乘试驾率:
                    {
                        return new List<String> { "试驾邀请书填写数", "首次来店客户登记数", "试乘试驾率" };
                    }
                case SellerDataItemType.试驾未成交率:
                    {
                        return new List<String> { "试驾客户未成交数", "试驾邀请书填写数", "试驾未成交率" };
                    }
                case SellerDataItemType.登记客户成交率:
                    {
                        return new List<String> { "当月累计交车数", "首次来店客户登记数", "登记客户成交率" };
                    }
                case SellerDataItemType.上月客户转化率:
                    {
                        return new List<String> { "上月遗留客户新增订单", "月初尚存意向客户数", "上月客户转化率" };
                    }
                case SellerDataItemType.超限价格成交率:
                    {
                        return new List<String> { "超限价格成交数", "当月累计交车数", "超限价格成交率" };
                    }
            }
            return new List<string>();
        }

        public List<RateDecimal> GetItemDatas(Employee seller)
        {
            SellerPersonReport report=getSellerPersonReport(seller.Id);
            switch (ItemType)
            {
                case SellerDataItemType.首次来店登记率:
                    {
                        RateDecimal v1 = new RateDecimal(report.LDJDPC);
                        RateDecimal v2 = new RateDecimal(report.SCLDKHDJS);
                        RateDecimal v3 = new RateDecimal(report.SCLDDJLV,true);
                        v3.IsFail = report.SCLDDJLV < report.SellerPerformanceStandard.SCLDDJLV;
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.试乘试驾率:
                    {
                        RateDecimal v1 = new RateDecimal(report.SJYQSTXS);
                        RateDecimal v2 = new RateDecimal(report.SCLDKHDJS);
                        RateDecimal v3 = new RateDecimal(report.SCSJLV, true);
                        v3.IsFail = report.SCSJLV < report.SellerPerformanceStandard.SCSJLV;
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.试驾未成交率:
                    {
                        RateDecimal v1 = new RateDecimal(report.SJKHCJS);
                        RateDecimal v2 = new RateDecimal(report.SJYQSTXS);
                        RateDecimal v3 = new RateDecimal(report.SCSJCJLV, true);
                        v3.IsFail = report.SCSJCJLV > report.SellerPerformanceStandard.SCSJCJLV;
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.登记客户成交率:
                    {
                        RateDecimal v1 = new RateDecimal(report.JCS);
                        RateDecimal v2 = new RateDecimal(report.SCLDKHDJS);
                        RateDecimal v3 = new RateDecimal(report.DJKHCJLV, true);
                        v3.IsFail = report.DJKHCJLV < report.SellerPerformanceStandard.DJKHCJLV;
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.上月客户转化率:
                    {
                        RateDecimal v1 = new RateDecimal(report.SYYLKHXZDD);
                        RateDecimal v2 = new RateDecimal(report.LastMonthPotentialClient);
                        RateDecimal v3 = new RateDecimal(report.SYKHZHLV, true);
                        v3.IsFail = report.SYKHZHLV < report.SellerPerformanceStandard.SYKHZHLV;
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.超限价格成交率:
                    {
                        RateDecimal v1 = new RateDecimal(report.CXJGCJS);
                        RateDecimal v2 = new RateDecimal(report.JCS);
                        RateDecimal v3 = new RateDecimal(report.CXJGCJLV, true);
                        v3.IsFail = report.CXJGCJLV > report.SellerPerformanceStandard.CXJGCJLV;
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
            }
            return new List<RateDecimal>();
        }


        private SellerPersonReport getSellerPersonReport(int sellid)
        {
            return SellerPersonReports.FirstOrDefault(t => t.Seller.Id == sellid);
        }

        public List<RateDecimal> GetAvgData()
        {
            switch (ItemType)
            {
                case SellerDataItemType.首次来店登记率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.LDJDPC));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SCLDKHDJS));
                        RateDecimal v3 = new RateDecimal(SellerReportModel.SCLDDJLV, true);
                        if (SellerPersonReports.Any())
                        {
                            v3.IsFail = v3.Value < SellerPersonReports[0].SellerPerformanceStandard.SCLDDJLV;
                        }
                        return new List<RateDecimal> { v1, v2, v3 };
                    }

                case SellerDataItemType.试乘试驾率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SJYQSTXS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SCLDKHDJS));
                        RateDecimal v3 = new RateDecimal(SellerReportModel.SCSJLV, true);
                        if (SellerPersonReports.Any())
                        {
                            v3.IsFail = v3.Value < SellerPersonReports[0].SellerPerformanceStandard.SCSJLV;
                        }
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.试驾未成交率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SJKHCJS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SJYQSTXS));
                        RateDecimal v3 = new RateDecimal(SellerReportModel.SCSJCJLV, true);
                        if (SellerPersonReports.Any())
                        {
                            v3.IsFail = v3.Value > SellerPersonReports[0].SellerPerformanceStandard.SCSJCJLV;
                        }
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.登记客户成交率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.JCS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SCLDKHDJS));
                        RateDecimal v3 = new RateDecimal(SellerReportModel.DJKHCJLV, true);
                        if (SellerPersonReports.Any())
                        {
                            v3.IsFail = v3.Value < SellerPersonReports[0].SellerPerformanceStandard.DJKHCJLV;
                        }
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.上月客户转化率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.SYYLKHXZDD));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.LastMonthPotentialClient));
                        RateDecimal v3 = new RateDecimal(SellerReportModel.SYKHZHLV, true);
                        if (SellerPersonReports.Any())
                        {
                            v3.IsFail = v3.Value < SellerPersonReports[0].SellerPerformanceStandard.SYKHZHLV;
                        }
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
                case SellerDataItemType.超限价格成交率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.CXJGCJS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.AverageOrDefault(t => t.JCS));
                        RateDecimal v3 = new RateDecimal(SellerReportModel.CXJGCJLV, true);
                        if (SellerPersonReports.Any())
                        {
                            v3.IsFail = v3.Value > SellerPersonReports[0].SellerPerformanceStandard.CXJGCJLV;
                        }
                        return new List<RateDecimal> { v1, v2, v3 };
                    }
            }
            return new List<RateDecimal>();
        }

        public List<RateDecimal> GetSumData()
        {
            switch (ItemType)
            {
                case SellerDataItemType.首次来店登记率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.Sum(t => t.LDJDPC));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.Sum(t => t.SCLDKHDJS));
                        return new List<RateDecimal> { v1, v2 };
                    }
                case SellerDataItemType.试乘试驾率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.Sum(t => t.SJYQSTXS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.Sum(t => t.SCLDKHDJS));
                        return new List<RateDecimal> { v1, v2 };
                    }
                case SellerDataItemType.试驾未成交率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.Sum(t => t.SJKHCJS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.Sum(t => t.SJYQSTXS));
                        return new List<RateDecimal> { v1, v2 };
                    }
                case SellerDataItemType.登记客户成交率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.Sum(t => t.JCS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.Sum(t => t.SCLDKHDJS));
                        return new List<RateDecimal> { v1, v2 };
                    }
                case SellerDataItemType.上月客户转化率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.Sum(t => t.SYYLKHXZDD));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.Sum(t => t.LastMonthPotentialClient));
                        return new List<RateDecimal> { v1, v2 };
                    }
                case SellerDataItemType.超限价格成交率:
                    {
                        RateDecimal v1 = new RateDecimal(SellerPersonReports.Sum(t => t.CXJGCJS));
                        RateDecimal v2 = new RateDecimal(SellerPersonReports.Sum(t => t.JCS));
                        return new List<RateDecimal> { v1, v2 };
                    }
            }
            return new List<RateDecimal>();
        }
    }

   
}